import 'package:mziot/app/config/api.dart';
import 'package:mziot/app/data/add_device_entity.dart';
import 'package:mziot/app/data/device_group_entity.dart';
import 'package:mziot/app/data/device_info_entity.dart';
import 'package:mziot/app/data/device_list_entity.dart';
import 'package:mziot/app/data/group_down_entity.dart';
import 'package:mziot/app/data/group_info_entity.dart';
import 'package:mziot/app/data/product_group_entity.dart';
import 'package:mziot/app/data/public_model.dart';
import 'package:mziot/app/tools/public_provider.dart';

class DeviceProvider {
  //获取mesh
  Future<BaseModel<String>?> takeMesh() {
    return PublicProvider.request<String>(path: Api.takeMesh);
  }

  //获取设备列表
  Future<BaseModel<DeviceListEntity>?> takeDeviceList(int type, int page) {
    return PublicProvider.request<DeviceListEntity>(
        path: Api.deviceList, params: {'currentPage': page, 'type': type});
  }

  //获取设备信息
  Future<BaseModel<DeviceInfoEntity>> takeDeviceInfo(int deviceId) {
    return PublicProvider.request<DeviceInfoEntity>(
        path: Api.deviceDetail, params: {'deviceId': deviceId});
  }

  //添加设备
  Future<BaseModel<AddDeviceEntity>?> addDevice(
      String deviceName, String bluetoothName, int? groupId, String? deviceIp) {
    var params = {};
    params['deviceName'] = deviceName;
    params['bluetoothName'] = bluetoothName;
    params['groupId'] = groupId;
    params['deviceIp'] = deviceIp;
    return PublicProvider.request<AddDeviceEntity>(path: Api.addDevice, params: params);
  }

  //编辑设备信息
  Future<BaseModel<dynamic>?> editDevice(int deviceId, String deviceName, int? groupId) {
    var params = {};
    params['deviceId'] = deviceId;
    params['deviceName'] = deviceName;
    params['groupId'] = groupId;
    return PublicProvider.request<dynamic>(path: Api.editDevice, params: params);
  }

  //获取设备组列表
  Future<BaseModel<DeviceGroupEntity>?> takeGroupList(int page) {
    return PublicProvider.request<DeviceGroupEntity>(
        path: Api.deviceGroup, params: {'currentPage': page});
  }

  //获取设备组下拉列表
  Future<BaseModel<List<GroupDownEntity>>?> takeGroupDownList() {
    return PublicProvider.request<List<GroupDownEntity>>(path: Api.deviceGroupList);
  }

  //获取设备组信息
  Future<BaseModel<GroupInfoEntity>> takeGroupInfo(int groupId) {
    return PublicProvider.request<GroupInfoEntity>(
        path: Api.deviceGroupDetail, params: {'groupId': groupId});
  }

  //编辑设备组信息
  Future<BaseModel<dynamic>?> editGroup(
      int? groupId, String groupName, List<int>? sceneIds, List<int>? deviceIds) {
    var params = {};
    params['groupId'] = groupId;
    params['groupName'] = groupName;
    params['sceneId'] = sceneIds;
    params['deviceIds'] = deviceIds;
    return PublicProvider.request<dynamic>(path: Api.addDeviceGroup, params: params);
  }

  //获取设备列表
  Future<BaseModel<DeviceListEntity>> takeGroupDevices(int groupId) {
    return PublicProvider.request<DeviceListEntity>(
        path: Api.groupDevices, params: {'groupId': groupId});
  }

  //获取设备列表
  Future<BaseModel<List<DeviceListList>>> takeDevices(int groupId) {
    return PublicProvider.request<List<DeviceListList>>(
        path: Api.allDevices, params: {'dgId': groupId});
  }

  //加入设备组
  Future<BaseModel<dynamic>> addGroup(int groupId, List<int> deviceIds) {
    return PublicProvider.request<dynamic>(
        path: Api.insertGroup, params: {'dgId': groupId, 'deviceIds': deviceIds});
  }

  //添加wifi
  Future<BaseModel<dynamic>> addWifi(String wifiName, String wifiPwd, int id) {
    return PublicProvider.request<dynamic>(
        path: Api.addWifi, params: {'wifiName': wifiName, 'wifiPwd': wifiPwd, 'deviceId': id});
  }

  //获取图片
  Future<BaseModel<ProductGroupProductVos?>> takePic(String name) {
    return PublicProvider.request<ProductGroupProductVos>(
        path: Api.productImage, params: {'bluetoothName': name});
  }

  //获取产品列表
  Future<BaseModel<List<ProductGroupEntity>>> takeProductList() {
    return PublicProvider.request<List<ProductGroupEntity>>(path: Api.productList);
  }

  //获取设备列表-组选设备用
  Future<BaseModel<List<DeviceListList>>> deviceList() {
    return PublicProvider.request<List<DeviceListList>>(path: Api.allDevices);
  }

  //删除设备
  Future<BaseModel<dynamic>> deleteDevice(List<int> ids) {
    return PublicProvider.request<dynamic>(path: Api.deleteDevice, params: {'deviceIds': ids});
  }

  //删除设备组
  Future<BaseModel<dynamic>> deleteGroup(List<int> ids) {
    return PublicProvider.request<dynamic>(path: Api.deleteGroup, params: {'deviceGroupIds': ids});
  }

  //移除设备组
  Future<BaseModel<dynamic>> leaveGroup(int groupId, int deviceId) {
    return PublicProvider.request<dynamic>(
        path: Api.leaveGroup, params: {'dgId': groupId, 'deviceId': deviceId});
  }

  //获取素有设备编号
  Future<BaseModel<List<dynamic>>> takeAllDevices() {
    return PublicProvider.request<List<dynamic>>(path: Api.allDevices);
  }

  //设备调光
  Future<BaseModel<dynamic>> lightingDevices({
    int? id,
    int light = 0,
    int? groupId,
  }) {
    var params = {};
    params['light'] = light;
    params['deviceId'] = id;
    params['groupId'] = groupId;
    return PublicProvider.request<dynamic>(path: Api.lightDevices, params: params);
  }

  //设备开关
  Future<BaseModel<dynamic>> switchDevices({bool isClose = false, int? id, int? groupId}) {
    return PublicProvider.request<dynamic>(
        path: Api.switchDevices, params: {'isClose': isClose, 'deviceId': id, 'groupId': groupId});
  }

  //更新设备信息
  static Future<BaseModel<dynamic>> updateDevices(String text) {
    return PublicProvider.request<dynamic>(
      path: Api.updateDevice,
      params: text,
      type: ContentType.text,
    );
  }

  //获取引导图
  static Future<BaseModel<List<dynamic>>> guideDance() {
    return PublicProvider.request<List<dynamic>>(path: Api.guideDance);
  }

  //设置时区
  static Future<BaseModel<dynamic>> updateZone(String code) {
    return PublicProvider.request<dynamic>(path: Api.setZone, params: {'timezone': code});
  }

  //拉取命令
  static Future<BaseModel<dynamic>> execCmd() {
    return PublicProvider.request<dynamic>(path: Api.cmdList);
  }
}
